var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient
var config = require('../config/db')
var passport = require('passport')

/*首页获取所有商品*/
router.get('/index',passport.authenticate('jwt', { session: false }),async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let products = await db.collection('indexproducts').find({ }).toArray()    //获取所有商品数据
  console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
/*饰品市场获取所有商品*/
router.get('/goods',passport.authenticate('jwt', { session: false }),async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let products = await db.collection('products').find({ }).toArray()    //获取所有商品数据
  console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
router.get('/getProductsPlace',async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let products = await db.collection('goodslnfo').aggregate([
      {'$group':{'_id':{'place':'$place'},'count':{'$sum':1}}}
  ]).toArray()    //获取所有商品数据
  console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
router.get('/getAllProducts',async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let products = await db.collection('products').find({ }).toArray()    //获取所有商品数据
  console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
router.get('/getAllBooks',async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let products = await db.collection('books').find({ }).toArray()    //获取所有商品数据
  console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
router.get('/searchBooks',async function(req, res, next) {
  let client = await MongoClient.connect(config.url)      //获取数据库连接对象
  let db = client.db()
  let seaarch = req.query.query
  console.log('111',seaarch)
  let products = await db.collection('books').find({"goodsName":{"$regex":`${seaarch}`}}).toArray()    //获取所有商品数据
  // console.log('products:',products)
  if (products.length > 0) {
    res.json({ code: 200, message: 'ok', data: products });   
  } else {
    res.json({ code: 200, message: 'error' });
  }
});
// /*获取商品详情 */
// router.get('/detail', function(req, res, next) {
//   var productId = parseInt(req.query.product_id)
//   //2.连接数据库
//   MongoClient.connect(config.url,function(err,client){
//     if(err){
//       console.log('err:',err)
//     }else{
//       var db = client.db()
//       //3.使用聚合,获取汇总数据
//       db.collection('productdetail').aggregate(
//          [
//   {$lookup:{
//     from:'users',
//     localField:'productId',
//     foreignField:'productId',
//     as:'products'
//   }},
//   {$match:{productId:productId}},
//   {$unwind:'$products'}
//       ]).toArray(function(err,result){
//         //4.响应请求
//         if(err){
//           console.log('err1',err)
//         }else{
//           console.log('res:',result)
//           res.json({code:200,message:'ok',data:result})
//           client.close()
//         }
//       })
//     }
//   })

// /* 卖家 */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
// /* 买家 */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
// /* 收藏 */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
// /* 求购 */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
// /* 出售 */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
// })
module.exports = router;
